﻿using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace XamlingCore.AzureHelper.TableStorage
{
    public abstract class TableBase<T> : TableServiceContext
    {
        private AzureTableHelper helper;

        private TableBase(string baseAddress, StorageCredentials credentials)
            : base(baseAddress, credentials)
        {

        }

        public TableBase(AzureTableHelper helper)
            : this(helper.CloudStorageAccount.TableEndpoint.ToString(), helper.CloudStorageAccount.Credentials)
        {
            this.helper = helper;
        }

        public TableBase(string tableName)
            : this(new AzureTableHelper(tableName))
        {

        }

        public void AttachObject(object obj)
        {
            AttachTo(helper.TableName, obj, "*");
        }

        public void AddObject(object obj)
        {
            AddObject(helper.TableName, obj);
        }

        public IQueryable<T> QueryObject
        {
            get
            {
                return CreateQuery<T>(helper.TableName);
            }
        }
    }
}
